package com.yoomiito.app.adapter.goods.detail;

import android.content.Context;
import androidx.annotation.NonNull;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.yoomiito.app.R;

public class GoodsDetailTitleAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private String title;

    public GoodsDetailTitleAdapter(Context context, String title) {
        mContext = context;
        this.title = title;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return new LinearLayoutHelper();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_goods_info_title,parent,false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder viewHolder = (ItemViewHolder) holder;
        if (TextUtils.isEmpty(title)){
            viewHolder.mView.setVisibility(View.GONE);
        }else {
            viewHolder.mView.setVisibility(View.VISIBLE);
            viewHolder.titleTv.setText(title);
        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    @Override
    public int getItemViewType(int position) {
        return 2;
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder{

        private TextView titleTv;
        private ConstraintLayout mView;
        public ItemViewHolder(View itemView) {
            super(itemView);
            mView = itemView.findViewById(R.id.item_goods_info_c);
            titleTv = itemView.findViewById(R.id.item_goods_info_title_tv);
        }
    }

    public void setNewTitle(String title) {
        this.title = title;
        notifyDataSetChanged();
    }
}
